import Mock from 'mockjs'
import {
  param2Obj
} from '../../untial/urlUtils.js'
var List = []
for (var i = 0; i < 100; i++) {
  List.push({
    idd: Mock.mock('@guid()'),
    id:List.length+1,
    name: Mock.mock('@cname'),
    nameid: Mock.mock('@date("yyyyMMdd")'),
    role: Mock.mock(
      "@natural(0,1)"
    ), //0 管理员  1 街道负责人
    explain: Mock.mock(
      "@natural(0,1)"
    ), //0 管理所有权限 1 上传车位信息
    date: Mock.mock('@datetime'),
    whether: Mock.mock(
      "@natural(0,1)"
    ), //0 否  1 是
  })
}

export default {
  getwxData: config => {
    // console.log('config',param2Obj(config.url));
    var { keyword,keyword1,page,pageSize,} = param2Obj(config.url)

    var res = List.filter(item => item.name.indexOf(keyword) != -1)
    if (keyword == 0) {
      var res = res.filter(item => item.nameid.indexOf(keyword1) != -1)
    }
    //page pageSize 是做分页的一个关键
    //(page - 1) * pageSize <= index < page * pageSize  0<=下标<20
    var resLimit =res.filter((item,index)=>{
      return (page-1) * pageSize <= index && index<page * pageSize
    })
    // List = res;
    console.log('config',config)
    console.log('res',List.length)
    return {
      code: 200,
      msg: "请求成功",
      data: resLimit,
      total:List.length,

    }
  },
  delwxId: config => {
    // console.log(config);
    // var id = JSON.parse(config.body).id
    var {
      idd
    } = JSON.parse(config.body)
    var res = List.filter(item => item.idd != idd)
    List = res
    return {
      code: 200,
      msg: "删除成功",
      data: null
    }
  },
  addwxId: config => {
    const {
      name,
      role,
      explain,
      whether,
    } = JSON.parse(config.body)
    List.splice(0,0,{
      idd: Mock.mock('@guid()'),
      id: 1,
      name: name,
      nameid: Mock.mock('@date("yyyyMMdd")'),
      role: role, //0 管理员  1 街道负责人
      explain: explain, //0 管理所有权限 1 上传车位信息
      date: Mock.mock('@now()'),
      whether: whether, //0 否  1 是
    })
    var sd=[]
    for(let i=1;i<100;i++){
      
      List[i].id = i +1;
    }
    sd.splice(1,0,List)
    console.log(sd)
    return {
      code: 200,
      msg: "添加成功",
      data: null

    }
  },
  updatewxId: config => {
    const {
      idd,
      id,
      name,
      role,
      explain,
      whether,
    } = JSON.parse(config.body);
    List.forEach(item=>{
      if(item.idd==idd){
        if(item.id==id){
        item.name = name
        item.role = role
        item.explain = explain
        item.date = Mock.mock('@now()')
        item.whether = whether
        return
      }
      }
  })
    return {
      code: 200,
      msg: "修改成功",
      data: null

    }
  }
}